﻿using System.Windows;
using _3DTicTacToe.Net;
using Microsoft.WindowsAPICodePack.Taskbar;

namespace _3DTicTacToe.UI
{
    partial class NetJoinPropertiesWindow : Window
    {
        private bool _showStartWindow = true;

        public NetJoinPropertiesWindow()
        {
            InitializeComponent();

            playerControl.PlayerPropsChanged += playerControl_PlayerPropsChanged;
        }

        void playerControl_PlayerPropsChanged(bool valid)
        {
            button1.IsEnabled = valid;
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            _showStartWindow = false;

            var connection = Connection.CreateClient();
            connection.Connect(hostTextBox.Text, int.Parse(portTextBox.Text));

            new ClientConnectingWindow(connection, playerControl.Player, hostTextBox.Text + ":" + portTextBox.Text).Show();

            if (TaskbarManager.IsPlatformSupported)
                TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.Indeterminate);
            
            Close();
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (_showStartWindow)
                new StartWindow().Show();
        }
    }
}
